package com.yunxi.service.dubboService.impl.sys;

import com.yunxi.model.sys.Menu;
import com.yunxi.model.sys.MenuAction;
import com.yunxi.service.localService.sys.MenuActionService;
import com.yunxi.service.localService.sys.MenuService;
import com.yunxi.sys.pojo.MenuActionVO;
import com.yunxi.sys.pojo.MenuVO;
import com.yunxi.sys.service.MenuDubboService;
import com.yunxi.util.RedisCacheService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by guan on 2018/9/29.
 */
public class MenuDubboServiceImpl implements MenuDubboService {

    @Autowired
    @Qualifier(value = "menuServiceImpl")
    MenuService menuService;

    @Autowired
    @Qualifier(value = "menuActionServiceImpl")
    MenuActionService menuActionService;

    @Autowired
    RedisCacheService redisCacheService;

    @Cacheable(value="menus",key = "'menu:data_'+#sys_type")
    public Object findTreeListBySysType(String sys_type) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return menuService.findTreeListBySysType(sys_type);
    }

    @Cacheable(value="menus",key = "'menu:all'")
    public Object findTreeList() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return menuService.findTreeList();
    }

    @Override
    public List getMenu() {
        return menuService.getMenu();
    }

    @CacheEvict(value="menus", allEntries=true)
    public int insert(MenuVO mo) throws InvocationTargetException, IllegalAccessException {
        Menu m = new Menu();
        BeanUtils.copyProperties(mo,m);
        return menuService.insert(m);
    }

    @Override
    public MenuVO getById(String menuId) throws InvocationTargetException, IllegalAccessException {
        Menu m = menuService.getById(menuId);
        MenuVO mo = new MenuVO();

        if(m.getParentAddr()!=null){
            String str = m.getParentAddr().substring(1,m.getParentAddr().length()-1);
            mo.setParentIdArr(str.replaceAll("\"","").split(","));
        }
        BeanUtils.copyProperties(m,mo);
        return mo;
    }

    @Override
    public void saveOrUpdate(MenuVO menu) throws InvocationTargetException, IllegalAccessException {
        Menu mo = new Menu();
        BeanUtils.copyProperties(menu,mo);
        menuService.saveOrUpdate(mo);
        if(menu.getActionList()!=null&&menu.getActionList().size()>0){

            menuActionService.delByMenuId(mo.getMenuId());

            for(int i=0; i<menu.getActionList().size();i++){
                MenuActionVO mav = menu.getActionList().get(i);
                MenuAction ma = new MenuAction();
                BeanUtils.copyProperties(mav,ma);
                menuActionService.save(ma);
            }
        }
        redisCacheService.delete("menus");
    }

    @Override
    public void removeById(String menuId) {
        menuService.removeById(menuId);
    }
}
